package com.mantru.iotpot.Cook.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mantru.iotpot.DateClass.pump_name_weight;
import com.mantru.iotpot.R;

import java.util.List;

public class OilPumpAdapter extends BaseAdapter {
    private Context context;
    private List<pump_name_weight> pumpList;
    private int selectedPosition = 0; // 记录当前选中的位置，初始值为-1表示没有选中

    public interface OnPumpSelectedListener {
        void onPumpSelected(String weight);
    }

    private OnPumpSelectedListener listener;

    public void setOnPumpSelectedListener(OnPumpSelectedListener listener) {
        this.listener = listener;
    }

    public OilPumpAdapter(Context context, List<pump_name_weight> pumpList) {
        this.context = context;
        this.pumpList = pumpList;
    }

    @Override
    public int getCount() {
        return pumpList.size();
    }

    @Override
    public Object getItem(int position) {
        return pumpList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.oil_pump_water_adapter, null);
        TextView textName = view.findViewById(R.id.oil_pump_text_name);
        TextView textWeight = view.findViewById(R.id.oil_pump_text_weight);
        LinearLayout linearLayout = view.findViewById(R.id.oil_pump_water);
        linearLayout.setOnClickListener(v -> {
            selectedPosition = position; // 更新选中位置
            notifyDataSetChanged(); // 通知适配器刷新
            // 调用接口方法，通知选中项的重量值
            if (listener != null) {
                String weight = pumpList.get(position).getWeight();
                listener.onPumpSelected(weight);
            }
        });
        pump_name_weight list = pumpList.get(position);
        textName.setText(list.getName());
        textWeight.setText(list.getWeight());
        // 根据是否选中设置背景
        if (selectedPosition == position) {
            linearLayout.setBackgroundResource(R.drawable.button12); // 高亮背景
        } else {
            linearLayout.setBackgroundResource(R.drawable.touch1); // 默认背景
        }
        return view;
    }

    // 提供一个方法来获取当前选中的位置
    public int getSelectedPosition() {
        return selectedPosition;
    }

    // 提供一个方法来设置当前选中的位置
    public void setSelectedPosition(int position) {
        selectedPosition = position;
        notifyDataSetChanged();
    }


    // 添加一个方法来更新选中项的文本
    public void updateSelectedText(String newText) {
        if (selectedPosition >= 0 && selectedPosition < pumpList.size()) {
            pump_name_weight selectedPump = pumpList.get(selectedPosition);
            selectedPump.setWeight(newText);
            notifyDataSetChanged(); // 刷新适配器
        }
    }
}
